package com.tudouji.project.document.controller;

import com.flow.service.mapper.IFileInfoMapper;
import com.flow.service.pojo.flow.FileInfo;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.document.domain.DocumentRecord;
import com.tudouji.project.document.service.IDocumentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单证跟踪记录Controller
 *
 * @author coal
 * @date 2021-12-07
 */
@RestController
@RequestMapping("/document/record")
public class DocumentRecordController extends BaseController {
	@Autowired
	private IDocumentRecordService documentRecordService;
	@Autowired
	private IFileInfoMapper fileInfoMapper;

	/**
	 * 查询单证跟踪记录列表
	 */
//	@PreAuthorize("@ss.hasPermi('document:record:list')")
	@GetMapping("/list")
	public TableDataInfo list(DocumentRecord documentRecord) {
		startPage();
		List<DocumentRecord> list = documentRecordService.selectDocumentRecordList(documentRecord);
		return getDataTable(list);
	}

	/**
	 * 导出单证跟踪记录列表
	 */
	@PreAuthorize("@ss.hasPermi('document:record:export')")
	@Log(title = "单证跟踪记录", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(DocumentRecord documentRecord) {
		List<DocumentRecord> list = documentRecordService.selectDocumentRecordList(documentRecord);
		ExcelUtil<DocumentRecord> util = new ExcelUtil<DocumentRecord>(DocumentRecord.class);
		return util.exportExcel(list, "单证跟踪记录数据");
	}

	/**
	 * 获取单证跟踪记录详细信息
	 */
	@PreAuthorize("@ss.hasPermi('document:record:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(documentRecordService.selectDocumentRecordById(id));
	}

	/**
	 * 新增单证跟踪记录
	 */
	@PreAuthorize("@ss.hasPermi('document:record:add')")
	@Log(title = "单证跟踪记录", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody DocumentRecord documentRecord) {
		return toAjax(documentRecordService.insertDocumentRecord(documentRecord));
	}

	/**
	 * 修改单证跟踪记录
	 */
	@PreAuthorize("@ss.hasPermi('document:record:edit')")
	@Log(title = "单证跟踪记录", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody DocumentRecord documentRecord) {
		return toAjax(documentRecordService.updateDocumentRecord(documentRecord));
	}

	/**
	 * 删除单证跟踪记录
	 */
	@PreAuthorize("@ss.hasPermi('document:record:remove')")
	@Log(title = "单证跟踪记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(documentRecordService.deleteDocumentRecordByIds(ids));
	}

	/**
	 * 获取单证跟踪记录文件
	 */
	@GetMapping("/getFileInfo")
	public AjaxResult getFileInfoByDocumentRecord(Long id) {
		FileInfo fileInfo = new FileInfo();
		fileInfo.setLinkedId(id);
		fileInfo.setType("document_record");
		return AjaxResult.success(fileInfoMapper.selectFileInfoList(fileInfo));
	}

	/**
	 * 删除单证跟踪记录
	 */
	@PreAuthorize("@ss.hasPermi('document:record:del')")
	@PostMapping("/del")
	public AjaxResult del(Long id) {
		return toAjax(documentRecordService.deleteDocumentRecordById(id));
	}

	/**
	 * 删除单证跟踪记录文件
	 */
	@PreAuthorize("@ss.hasPermi('document:record:delFileInfo')")
	@PostMapping("/delFileInfo")
	public AjaxResult delFileInfo(Long id) {
		return toAjax(fileInfoMapper.deleteFileInfoById(id));
	}

	@GetMapping("/getListByNodeTable")
	public AjaxResult list(Long orderId,String busTable) {
		DocumentRecord documentRecord =new DocumentRecord();
		documentRecord.setBusTable(busTable);
		documentRecord.setOrderId(orderId);
		List<DocumentRecord> list = documentRecordService.selectDocumentRecords(documentRecord);
		return AjaxResult.success(list);
	}
}
